<template>
  <div class="root">
    <div class="login-container">
      <h1 class="title">登录</h1>

      <el-form
        ref="loginForm"
        :model="user"
        status-icon
        :rules="rules"
        label-width="100px"
        class="login-form"
      >
        <el-form-item label="账号" prop="account">
          <el-input v-model.number="user.account" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="user.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="custom-button" @click.native.prevent="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>

      <div class="register-link">
        还没有账号？<span class="register-text" @click="toRegister">立即注册</span>
      </div>
    </div>

    <el-dialog
      :visible.sync="dialogVisible"
      width="400px"
    >
      <h1 class="title">注册</h1>

      <div class="register-container">
        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" label-width="100px" class="register-form">
          <el-form-item label="用户名" prop="memberName">
            <el-input v-model="registerForm.memberName" class="custom-input" />
          </el-form-item>
          <el-form-item label="账号" prop="account">
            <el-input v-model="registerForm.account" class="custom-input" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="registerForm.password" type="password" class="custom-input" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="register">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { register } from '@/api/user'

export default {
  data() {
    return {
      dialogVisible: false,
      registerForm: {
        memberName: '',
        account: '',
        password: ''
      },
      registerRules: {
        memberName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      user: {},
      rules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    register() {
      register(this.registerForm).then(res => {
        if (res.code === 200) {
          this.dialogVisible = false
          this.$message.success('注册成功')
        }
      })
    },
    toRegister() {
      this.dialogVisible = true
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$store.dispatch('user/login', this.user).then(() => {
            this.$router.push({ path: this.redirect || '/' })
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.register-form{
  text-align: center;
}
.login-container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.login-form {
  width: 300px;
  margin-top: 20px;
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
}

.title {
  font-size: 24px;
  color: #333;
  text-align: center;
  margin-bottom: 20px;
}

.custom-button {
  width: 100%;
}

.register-link {
  margin-top: 20px;
  text-align: center;
}

.register-text {
  color: #409eff;
  cursor: pointer;
}

.register-container {
  margin-left: 30px;
  text-align: center;
  width: 300px;
  margin-top: 20px;
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
}

.custom-input {
  width: 100%;
}
</style>
